package org.shangma.feign.fallback;

import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.tm.api.GlobalTransactionContext;
import org.shangma.common.Account;
import org.shangma.feign.AccountFeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

@Component
public class AccountFeignClientFallback implements AccountFeignClient {

    // 降级方法
    @Override
    public void reduceMoney(@RequestParam("accountId") int accountId, @RequestParam("money") double money) {
        System.out.println("reduceMoney降级执行");
        if(RootContext.inGlobalTransaction()){
            try {
                GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            } catch (TransactionException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
